package com.skyworth.ui.customview;

import java.util.HashMap;

/**
 * Created by luwei on 15-11-24.
 */
public class ViewFactory{
    private static HashMap<Class,Object> factoryCache = new HashMap<Class,Object>();
    public static synchronized <T> T getFactory(Class<? extends T> clazz){
        if (factoryCache.get(clazz) == null){
            try {
                T factory = (T) clazz.newInstance();
                factoryCache.put(clazz,factory);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("cannot init factory by clazz");
            }
        }
        return (T)factoryCache.get(clazz);
    }

    public synchronized static void destroy(){
        if (factoryCache != null)
            factoryCache.clear();
    }

}
